package me.report.api.utils;

import java.io.File;
import java.io.IOException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Configuration extends PropertiesConfiguration {

    private static Log logger = LogFactory.getLog(Configuration.class);
    private static final String PROPS_RESOURCE = "mongodb.properties";
    private static Configuration instance = null;
    private String propsFile = PROPS_RESOURCE;

    public static Configuration getInstance() {
        if (instance == null) {
            instance = new Configuration();
            instance.loadProperties();
        }
        return instance;
    }

    private void loadProperties() {
        try {
            File is;
            // Class c = getClass();
            is = new File(propsFile);
            if (is != null) {
                loadProperties(is);
            }
        } catch (IOException ioe) {
        }
    }

    private void loadProperties(File is)
            throws IOException {
        try {
            instance.setFile(is);
            instance.setReloadingStrategy(new FileChangedReloadingStrategy());
            instance.setAutoSave(true);
            instance.load();
            logger.info("Loaded engine properties from " + propsFile);
        } catch (Exception e) {
            throw new IOException("Failed to load properties from " + propsFile);
        }
    }
}
